//
//  WBStatusPictureView.swift
//  weibo
//
//  Created by iobit on 2017/07/07.
//  Copyright © 2017 zhuxu. All rights reserved.
//

import UIKit

class WBStatusPictureView: UIView {

        var urls: [WBStatusPicture]? {
        
        didSet {
            for view in subviews {
                view.isHidden = true
            }
            guard let urls = urls else {
                return
            }
            var index = 0
            
            for model in urls {
                let iv = subviews[index] as! UIImageView
                // 判断图片为4的情况
                if index == 1 && urls.count == 4 {
                    index += 1
                }
                iv.cz_setImage(urlString: model.thumbnail_pic, placeholderImage: nil)
                iv.isHidden = false
                index += 1
                
            }
            
        }
        
    }
    @IBOutlet weak var heightCons: NSLayoutConstraint!
    
    override func awakeFromNib() {
        setupUI()
    }

}

extension WBStatusPictureView {
    
    func setupUI()  {
        
        clipsToBounds = true
        backgroundColor = superview?.backgroundColor
        
        let count = 3
        
        let rect = CGRect(x: 0, y: WBStatusPictureViewOutterMargin, width: WBStatusPictureViewItemWidth, height: WBStatusPictureViewItemWidth)
        
        for i in 0..<count * count {
            
            let vi = UIImageView()

            
            vi.contentMode = .scaleAspectFill
            vi.clipsToBounds = true //超过边界的内容不显示
            
            //行
            let row = CGFloat(i / count)
            //列
            let col = CGFloat(i % count)
            
            let xOffest = col * (WBStatusPictureViewItemWidth + WBStatusPictureViewInnerMargin)
            
            let yOffect = row * (WBStatusPictureViewItemWidth + WBStatusPictureViewInnerMargin)
            
            vi.frame = rect.offsetBy(dx: xOffest, dy: yOffect)
            
            addSubview(vi)
        }
        
    }
    
}

